package com.hs.mesj.controller;


import com.config.ContantValue;
import com.hs.mesj.socket.ApplicationProUtil;
import com.hs.mesj.socket.WebSocketServer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * WebSocketController
 * @author zhengkai.blog.csdn.net
 */
@RestController
public class DemoController {
    public  static Log log = LogFactory.getLog(DemoController.class);

    @GetMapping("hello")
    public ResponseEntity<String> hello(){
        return ResponseEntity.ok("请求成功");
    }
    @GetMapping("index")
    public ModelAndView index(Model model){
        model.addAttribute("name","XX");

        return new ModelAndView("index.html");
    }

    @GetMapping("page")
    public ModelAndView page(Model model){
        model.addAttribute("distValue", ContantValue.distValue);
        return new ModelAndView("websocket.html");
    }
    @RequestMapping("out")
    public ModelAndView out(Model model,HttpServletRequest request){
        request.getSession().invalidate();
        return new ModelAndView("login.html");
    }

    /**
     * 获取初始化值 model 即 ContantValue.distValue
     * @param model
     * @return
     */
    @RequestMapping("/getModel")
    @ResponseBody
    public Map getModel(Model model){
//        model.addAttribute("distValue", ContantValue.distValue);
        return ContantValue.distValue;
    }
    @GetMapping("mainShow")
    public ModelAndView main(Model model,HttpServletRequest request){
        model.addAttribute("path", ApplicationProUtil.getValue("web-site"));
        Object user = request.getSession().getAttribute("user");
        model.addAttribute("distValue", ContantValue.distValue);
        if(!user.equals("admin")){
            return new ModelAndView("show.html");
        }
        return new ModelAndView("main_show.html");
    }
    @GetMapping("mainControl")
    public ModelAndView mainControl(Model model,HttpServletRequest request){
        Object user = request.getSession().getAttribute("user");
        model.addAttribute("path", ApplicationProUtil.getValue("web-site"));
        model.addAttribute("distValue", ContantValue.distValue);
        if(!user.equals("admin")){
            return new ModelAndView("show.html");
        }
        return new ModelAndView("main_control.html");
    }
    @RequestMapping("log")
    public ModelAndView log(Model model, HttpServletRequest request){
        return new ModelAndView("login.html");
    }
    @RequestMapping("login")
    public ModelAndView login(Model model, HttpServletRequest request){
        model.addAttribute("distValue", ContantValue.distValue);
        model.addAttribute("path", ApplicationProUtil.getValue("web-site"));

        Object name = request.getParameter("name");
        Object pass = request.getParameter("pass");
        if(name==null || pass== null){
            model.addAttribute("msg", "账号密码不能为空！");
            return new ModelAndView("login.html");
        }
        if(name.equals("admin") && pass.equals("admin123.")){
            request.getSession().setAttribute("user","admin");

            return new ModelAndView("main_show.html");
        }else{
            model.addAttribute("msg", "密码错误！");
        }
        if(name.equals("student") && pass.equals("123")){
            request.getSession().setAttribute("user","student");
            return new ModelAndView("show.html");
        }else{
            model.addAttribute("msg", "密码错误！");
        }
        return new ModelAndView("login.html");
    }

    @RequestMapping("/push/{toUserId}")
    public ResponseEntity<String> pushToWeb(String message, @PathVariable String toUserId) throws IOException {
        log.info("访问推送push/id");
        WebSocketServer.sendInfo(message,toUserId);
        return ResponseEntity.ok("MSG SEND SUCCESS");
    }
//    //收到 推送所有WEB
//    @RequestMapping("/pushAll")
//    public ResponseEntity<String> pushToWeb(String message) throws IOException {
//        log.info("访问推送 pushAll");
//        String[] srta = message.split("\\+");
//        Map map = new HashMap();
//        map.put(srta[1],srta[0]);
//        WebSocketServer.sendInfoAll(map.toString());
//        return ResponseEntity.ok("MSG SEND SUCCESS");
//    }
}

